#include "jabstractsocket.h"
#include "jabstractio_p.h"

namespace JXQ
{

typedef JAbstractSocket::Type  Type;

class J_DECL_HIDDEN JAbstractSocketPrivate : public JAbstractIOPrivate
{
	J_DECLARE_PUBLIC(JAbstractSocket)

public:
	using JAbstractIOPrivate::JAbstractIOPrivate;

public:
	Type type;
};

/*------------------------------------------------------------------------*/

JAbstractSocket::JAbstractSocket(Type type) :
	JAbstractIO(new JAbstractSocketPrivate(this))
{
	d_func()->type = type;
}

JAbstractSocket::~JAbstractSocket()
{

}

bool JAbstractSocket::open(OpenMode mode)
{
	J_UNUSED(mode);
	return false;
}

bool JAbstractSocket::open(int handle, OpenMode mode)
{
	J_UNUSED(handle);
	J_UNUSED(mode);
	return false;
}

bool JAbstractSocket::close()
{
	return false;
}

int64_t JAbstractSocket::ioctl(unsigned int cmd, void *buf)
{
	J_UNUSED(cmd);
	J_UNUSED(buf);
	return -1;
}

void JAbstractSocket::flush()
{

}

int64_t JAbstractSocket::bytesAvailable() const
{
	return -1;
}

int64_t JAbstractSocket::size() const
{
	return 0;
}

bool JAbstractSocket::bind(uint16_t port)
{
	J_UNUSED(port);
	return false;
}

Type JAbstractSocket::type() const
{
	return d_func()->type;
}

JHandle JAbstractSocket::handle() const
{
	return 0;
}

int64_t JAbstractSocket::virtualRead(int64_t bufSize, void *buffer)
{
	J_UNUSED(buffer);
	J_UNUSED(bufSize);

	setErrno(0);

	if( d_ptr->check(ReadOnly) == false )
		return -1;

	return 0;
}

int64_t JAbstractSocket::virtualWrite(int64_t bufSize, const void *buffer)
{
	J_UNUSED(buffer);
	J_UNUSED(bufSize);

	setErrno(0);

	if( d_ptr->check(WriteOnly) == false )
		return -1;

	return 0;
}

} // namespace JXQ
